package cc.rengu.igas.bomp.core.realize.impl;

import cc.rengu.igas.bomp.common.dao.IndustryMapInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.IndustryMapInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.IndustryMapInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.enums.SysParamEnum;
import cc.rengu.igas.bomp.core.realize.WechatActivityService;
import cc.rengu.igas.bomp.facade.enums.BompMchntNatureEnum;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信活动服务
 *
 * @version 1.0
 * @date 2019/08/15
 */
public class WechatActivityServiceImpl implements WechatActivityService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public List<String> getAllActivityList(String instId) throws Exception {
        SysParam sysParam = SysParamEnum.getSysParam(instId, SysParamEnum.ACTIVITY_LIST);
        if (null == sysParam) {
            rglog.error("未配置活动.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动");
        }

        return Arrays.asList(sysParam.getParamValue().split(","));
    }

    @Override
    public List<String> getActivitySupportNatureAndIndustryType(String instId, String activityId) throws Exception {
        List<String> natureAndIndustryTypeList = new ArrayList<>();
        SysParam supportSysParam = SysParamEnum.getSysParam(instId, activityId, SysParamEnum.SUPPORT_SUBJECT_TYPE.getParamKey());
        if (null == supportSysParam) {
            rglog.error("未配置活动<{}>支持商户类型.", activityId);
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动支持商户类型");
        }

        String[] supprotSubjects = supportSysParam.getParamValue().split(",");
        for (String supprotSubject : supprotSubjects) {
            String[] array = supprotSubject.split("-");
            String mchntNature = BompMchntNatureEnum.getMchntNatureByWechatMchntType(array[0]);

            IndustryMapInfoMapper industryMapInfoMapper = new IndustryMapInfoMapperImpl();
            List<IndustryMapInfo> industryMapInfoList = industryMapInfoMapper.selectIndustryMapInfoByDestCatalogCd(ChannelEnum.WXZF.getChannelType(), null);
            List<IndustryMapInfo> industryMapInfos = industryMapInfoList.stream().filter(item -> array[0].equals(item.getPublicPrivateFlag())
                    && array[1].equals(item.getDestCatalogCd())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(industryMapInfos)) {
                for (IndustryMapInfo industryMapInfo : industryMapInfos) {
                    natureAndIndustryTypeList.add(mchntNature + "-" + industryMapInfo.getIndustryType());
                }
            }
        }
        natureAndIndustryTypeList = natureAndIndustryTypeList.stream().distinct().collect(Collectors.toList());
        return natureAndIndustryTypeList;
    }

    @Override
    public List<String> getMchntCanApplyActivity(MchntBaseInfo mchntBaseInfo) throws Exception {
        BompMchntNatureEnum bompMchntNatureEnum = BompMchntNatureEnum.getBompMchntNatureEnumByType(mchntBaseInfo.getMchntNature());
        String wechatMchntType = bompMchntNatureEnum.getWechatPublicPrivateKey();

        IndustryMapInfoMapper industryMapInfoMapper = new IndustryMapInfoMapperImpl();
        IndustryMapInfo industryMapInfo = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(), ChannelEnum.WXZF.getChannelType(), wechatMchntType);
        if (null == industryMapInfo) {
            industryMapInfo = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(mchntBaseInfo.getIndustryType(),
                    ChannelEnum.WXZF.getChannelType(), BompMchntNatureEnum.ENTERPRISE.getWechatPublicPrivateKey());
            if (null == industryMapInfo) {
                rglog.error("未配置商户性质<{}>-微信商户类型<{}>对应微信行业类别.", mchntBaseInfo.getIndustryType(), wechatMchntType);
                throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动支持商户类型");
            }
        }
        String wechatSettleId = industryMapInfo.getDestCatalogCd();

        /*活动支持key*/
        String activitySupportKey = wechatMchntType + "-" + wechatSettleId;

        List<SysParam> supportSubjectSysParamList = SysParamEnum.getSysParamListByParamKey(mchntBaseInfo.getInstId(), SysParamEnum.SUPPORT_SUBJECT_TYPE.getParamKey());
        if (CollectionUtils.isEmpty(supportSubjectSysParamList)) {
            rglog.error("未配置活动支持商户类型.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未配置活动支持商户类型");
        }
        List<String> activityIdList = new ArrayList<>();
        for (SysParam sysParam : supportSubjectSysParamList) {
            if (sysParam.getParamValue().contains(activitySupportKey)) {
                activityIdList.add(sysParam.getParamType());
            }
        }
        if (CollectionUtils.isEmpty(activityIdList)) {
            rglog.error("无商户所支持的微信活动.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "无商户所支持的微信活动");
        }

        return activityIdList;
    }
}
